<template>
  <!-- 模具状态变更记录表单弹窗 -->
  <div>
    <el-dialog
      :close-on-click-modal="false"
      v-model="visibleDialog"
      :title="props.title"
      :width="!props.isReadonly ? '700px' : '750px'"
      draggable
      @close="closeDialog"
      destroy-on-close
    >
      <el-form
        ref="formRef"
        :model="formData"
        :rules="rules"
        label-width="110"
        v-show="!props.isReadonly"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="模具名称" prop="assetName">
              <el-input
                v-if="!props.isReadonly"
                v-model="formData.assetName"
                clearable
                placeholder="请输入模具名称"
                @focus="openSelectDialog()"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="模具编号" prop="assetCode">
              <el-input
                v-if="!props.isReadonly"
                v-model="formData.assetCode"
                placeholder="请输入模具编号"
                clearable
                disabled
              />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="变更前状态" prop="statusBeforeChange">
              <el-select
                v-if="!props.isReadonly"
                v-model="formData.statusBeforeChange"
                placeholder="请选择变更前状态"
                style="width: 300px"
                clearable
                disabled
              >
                <el-option
                  v-for="item in dictionaryStore.filterDictList('MouldStatus')"
                  :key="item.value"
                  :label="item.label"
                  :value="String(item.value)"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="变更后状态" prop="statusAfterChange">
              <el-select
                v-if="!props.isReadonly"
                v-model="formData.statusAfterChange"
                placeholder="请选择变更后状态"
                style="width: 300px"
                clearable
              >
                <el-option
                  v-for="item in dictionaryStore.filterDictList('MouldStatus')"
                  :key="item.value"
                  :label="item.label"
                  :value="Number(item.value)"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单据号">
              <el-input
                v-if="!props.isReadonly"
                v-model="formData.recordCode"
                placeholder="请输入单据号"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="变更原因" prop="changeReason">
              <el-input
                v-if="!props.isReadonly"
                v-model="formData.changeReason"
                placeholder="请输入变更原因"
                clearable
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-descriptions
        v-show="props.isReadonly"
        class="margin-top"
        title=""
        :column="2"
        size="large"
        border
      >
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">模具名称</div>
          </template>
          {{ formData.assetName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">模具编号</div>
          </template>
          {{ formData.assetCode }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">变更前状态</div>
          </template>
          {{
            dictionaryStore.filterDictName(
              "MouldStatus",
              formData.statusBeforeChange
            )
          }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">变更后状态</div>
          </template>
          {{
            dictionaryStore.filterDictName(
              "MouldStatus",
              formData.statusAfterChange
            )
          }}
        </el-descriptions-item>

        <el-descriptions-item>
          <template #label>
            <div class="cell-item">单据号</div>
          </template>
          {{ formData.recordCode }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">变更原因</div>
          </template>
          {{ formData.changeReason }}
        </el-descriptions-item>
      </el-descriptions>
      <template #footer>
        <div class="dialog-footer">
          <el-button
            v-if="!props.isReadonly"
            type="primary"
            :loading="loading"
            @click="handleSubmit"
            >确 定</el-button
          >
          <el-button @click="closeDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <moldicechangestatus-select
      :visible="selectDialog.visible"
      @change-visible="changeVisible"
    />
  </div>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "MOLDDEVICECHANGESTATUSEDIT",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

const rules = reactive({
  version: [],
  recordCode: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  originalVoucherNo: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  assetCode: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  assetName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  changeType: [
    { min: 1, max: 10, message: "长度在 1 到 10 个字符", trigger: "blur" },
  ],
  changeReason: [
    { required: true, message: "请输入变更原因", trigger: "blur" },
  ],
  statusBeforeChange: [
    { required: true, message: "请选择变更前状态", trigger: "change" },
  ],
  statusAfterChange: [
    { required: true, message: "请选择变更后状态", trigger: "change" },
  ],
  remarks: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
  createTime: [],
  createdUserId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  createdUserName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  lastUpdateTime: [],
  lastUpdatedUserId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  lastUpdatedUserName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  version: "",
  recordCode: "",
  originalVoucherNo: "",
  assetKind: "",
  assetId: "",
  assetCode: "",
  assetName: "",
  changeType: "",
  changeReason: "",
  statusBeforeChange: "",
  statusAfterChange: "",
  remarks: "",
  organizationId: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        version,
        recordCode,
        originalVoucherNo,
        assetKind,
        assetId,
        assetCode,
        assetName,
        changeType,
        changeReason,
        statusBeforeChange,
        statusAfterChange,
        remarks,
        organizationId,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
      } = newVal;
      Object.assign(formData, {
        id,
        version,
        recordCode,
        originalVoucherNo,
        assetKind,
        assetId,
        assetCode,
        assetName,
        changeType,
        changeReason,
        statusBeforeChange,
        statusAfterChange,
        remarks,
        organizationId,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.version = "";
  formData.recordCode = "";
  formData.originalVoucherNo = "";
  formData.assetKind = "";
  formData.assetId = "";
  formData.assetCode = "";
  formData.assetName = "";
  formData.changeType = "";
  formData.changeReason = "";
  formData.statusBeforeChange = "";
  formData.statusAfterChange = "";
  formData.remarks = "";
  formData.organizationId = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
}

const loading = ref(false);

/** 模具状态变更记录保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/eam/mouldStatusChange/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 目录树
const catalogList: any = ref([]);

// 获取内容树数据
const getCAtalogList = async () => {
  // 获取内容树
  await request({
    url: "eam/mouldAssetType/getAvailableTree",
    method: "POST",
    data: {},
  }).then((res) => {
    catalogList.value = res.data;
  });
};

const handleNodeClick = (e) => {
  console.log(e);
};

// 点击模具

function openSelectDialog() {
  changeVisible(true, null);
}
//模具显隐
const selectDialog = ref({
  visible: false,
});
const changeVisible = (val: boolean, data: any) => {
  if (data) {
    formData.assetId = data.id;
    formData.assetCode = data.assetCode;
    formData.assetName = data.assetName;
    formData.statusBeforeChange = String(data.assetStatus);
  }
  selectDialog.value.visible = val;
};
onMounted(() => {
  getCAtalogList();
});
// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
